function updateGamePlayers(player, operation) {
	var currentPlayerCount = Object.size(playerMap);
	var newPlayerCount = currentPlayerCount - 1;
	if (operation == "add") {
      newPlayerCount = currentPlayerCount + 1;
	}
	doTheShuffle(currentPlayerCount, newPlayerCount, player);
}

function doTheShuffle(currentPlayerCount, newPlayerCount, player) {
	$(".player_pos").removeClass("returning");
	$(".player_pos").fadeOut("slow");
	for (var i = 1; i < currentPlayerCount; i++) {
		var newPosition = $("." + newPlayerCount + "-player-" + i);
		var self = $("." + currentPlayerCount + "-player-" + i);
		var selfClone = self.clone();
		selfClone.appendTo(newPosition.parent());
		selfClone.attr("class", newPosition.attr("class"));
		newPosition.appendTo(self.parent());
		newPosition.attr("class", self.attr("class"));
		self.remove();
		selfClone.addClass("returning");
	}
	$(".returning").fadeIn("slow");
	$.post("/getNameAndAvatar/", {"player_id": player})
    .success(function(data) {
    	var playerName = data["name"];
    	loadPlayer(newPlayerCount, player, data["avatar"], newPlayerCount - 1, playerName,
    	  "connected", "connected");
    	processUpdate(player, "Joined the game", dateFormat(new Date(), "h:MM TT"),
    	  playerName, "game", data["avatar"]);
    })
    .error(function(error) {
    	loadPlayer(newPlayerCount, player, null, newPlayerCount - 1, playerName, "connected",
    	    "connected");
    });
}

function loadPlayer(totalPlayers, playerId, avatar, playerCount, playerName, gameStatus, playerStatus) {
  playerMap[playerId] = playerName;
  avatarMap[playerId] = avatar;
  var connectionColor = "lightgrey";
  if (gameStatus == "connected") {
	  connectionColor = "green";
  }
  else if (playerStatus == "connected") {
	  connectionColor = "#f89406"
  }
  $("." + totalPlayers + "-player-" + playerCount).addClass("profile-thumbnail-" + playerId);
  $("." + totalPlayers + "-player-" + playerCount).append("<div class='label-square-bottom label-info " +
		  "username_of_" + playerId + "' style='width:auto;'>" + playerName +
		  "<svg title xmlns='http://www.w3.org/2000/svg' version='1.1' style='width:16px;height:16px;float:right;margin-top:1px'>" +
			"   <circle id='status_of_" + playerId + "' cx='8' cy='4' r='4' " +
			"fill='" + connectionColor + "' />" +
		  "</svg></div>");

  var playerStatsDivClone = $("[name='player-stats-div']").clone();
  playerStatsDivClone.removeAttr("name");

  playerStatsDivClone.find("[name='playing_span']").attr("id", "playing_of_" + playerId);
  playerStatsDivClone.find("[name='lead_span']").attr("id", "lead_of_" + playerId);
  playerStatsDivClone.find("[name='dealer_span']").attr("id", "dealer_of_" + playerId);

  playerStatsDivClone.find("[name='score_span']").attr("id", "score_of_" + playerId);
  playerStatsDivClone.find("[name='won_span']").attr("id", "won_of_" + playerId);

  var avatarHolder = playerStatsDivClone.find("[name='avatar-div']");
  avatarHolder.append("<img class='game-avatar avatar_of_" + playerId + "' " +
		  "src='" + avatar + "' style='margin-left:0px'></img>");
  playerStatsDivClone.removeClass("hide");

  $("." + totalPlayers + "-player-" + playerCount).append(playerStatsDivClone);

  $("." + totalPlayers + "-player-" + playerCount).append(
		  "<div class='square-top' id='played-cards-" + playerId + "' " +
		  "style='height:47px;background-color:white;'>" +
		  "<img id='played-card-" + playerId + "-0' style='width:31px;height:45px;margin:1px; opacity:0.1;border: 1px solid lightgrey' " +
		  		"src='/site_media/images/cards/back.png' class='card-back played_card played-card-" + playerId + "'>" +
		  "<img id='played-card-" + playerId + "-1' style='width:31px;height:45px;margin:1px; opacity:0.1;border: 1px solid lightgrey' " +
		  		"src='/site_media/images/cards/back.png' class='card-back played_card played-card-" + playerId + "'>" +
		  "<img id='played-card-" + playerId + "-2' style='width:31px;height:45px;margin:1px; opacity:0.1;border: 1px solid lightgrey' " +
		  		"src='/site_media/images/cards/back.png' class='card-back played_card played-card-" + playerId + "'>" +
		  "<img id='played-card-" + playerId + "-3' style='width:31px;height:45px;margin:1px; opacity:0.1;border: 1px solid lightgrey' " +
		  		"src='/site_media/images/cards/back.png' class='card-back played_card played-card-" + playerId + "'>" +
		  "<img id='played-card-" + playerId + "-4' style='width:31px;height:45px;margin:1px; opacity:0.1;border: 1px solid lightgrey' " +
		  		"src='/site_media/images/cards/back.png' class='card-back played_card played-card-" + playerId + "'>" +
		  "</div>");

  $("#avatar-info-div-" + playerId).append($("[name='player-stats-div']").html());

  $("." + totalPlayers + "-player-" + playerCount).fadeIn("slow");

}

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};